export default {
  data() {
    return {
      loading: true, // 遮罩层
      tableData: [], // 表格数据
      params: {},
      total: 0, // 列表总数
      page: { // 分页
        pageNo: 1,
        pageSize: 30,
        inputNum: 30
      },
      edit: {
        textMap: {
          update: 'editrole',
          create: 'addrole'
        },
        rules: {
          power: [{ required: true, message: 'title is required', trigger: 'change' }],
          roleName: [{ required: true, message: 'title is required', trigger: 'blur' }]
        },
        temp: {
          id: null,
          roleName: '',
          status: 1,
          power: [],
          desc: ''
        },
        dialogStatus: 'create',
        visible: false
      }
    }
  },
  methods: {
    initTableData(url, params) {
      this.params = params
      if (!params.pageSize) {
        params.pageSize = 30
      }
      this.page.pageSize = this.page.inputNum = params.pageSize || 30
      this.listUrl = url
      this.getTableData(params)
    },
    // 获取表格数据
    getTableData(params) {
      console.log(params)
      this.loading = true
      this.$request({
        url: this.listUrl,
        data: params
      })
        .then(res => {
          this.loading = false
          if (res.code === 200) {
            if (res.total !== undefined) {
              this.total = res.total
            }
            this.tableData = res.items
          } else {
            this.tableData = []
          }
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 获取翻页数据
    getPages(pageNo, pageSize) {
      this.params.pageNo = pageNo
      this.params.pageSize = pageSize
      this.getTableData(this.params)
    },
    // 条件查询
    query(res) {
      const params = Object.assign({}, this.params, res)
      this.getTableData(params)
    },
    buttonRatch(row, type) {
      console.log(row, type);
      (type === 'delete') && this.deleteBatch();
      (type === 'edit') && this.editBatch();
      (type === 'add') && this.addBatch()
    },
    checkIds(val) {
      const ids = []
      for (const i in val) {
        ids.push(val[i].id)
      }
      this.ids = ids.join(',')
      this.disabled = !(val.length > 0)
      this.checkedList = val
    },
    // 获取选中id和状态status
    checkIdsAndStatus(val) {
      const ids = []
      const status = []
      for (const i in val) {
        ids.push(val[i].id)
        status.push(val[i].status)
      }
      this.ids = ids.join(',')
      this.status = status.join(',')
      this.disabled = !(val.length > 0)
      this.checkedList = val
    },
    deleteBatch(url, ids) { // 删除
      this.$confirm('是否确定删除？', '警告', { type: 'error' })
        .then(mes => {
          this.$http.post(url, { ids: ids }).then(res => {
            if (res.code === '200') {
              this.successMessage('删除成功')
              this.getTableData(this.params)
            }
          })
        })
    },
    editBatch(row) {
      this.edit.visible = true
      this.edit.temp = Object.assign({}, row) // copy obj
      this.edit.dialogStatus = 'update'
      this.edit.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    addBatch() {
      this.edit.visible = true
    },
    removeBatch(url, roleId, ids) { // 移除
      this.$confirm('确定移除吗？', '提示', { type: 'error' })
        .then(mes => {
          this.$http.post(url, { roleId: roleId, userIds: ids }).then(res => {
            if (res.code === '200') {
              this.successMessage('移除成功')
              this.getTableData(this.params)
            }
          })
        })
    },
    priorityBatch(url, ids, priorities, regDefId) { // 保存排列循序
      this.$confirm('是否保存排列顺序', '提示', { type: 'warning' })
        .then(mes => {
          this.$http.post(url, { ids: ids, priorities: priorities, regDefId: regDefId }).then(res => {
            if (res.code === '200') {
              this.successMessage('操作成功')
              this.getTableData(this.params)
            }
          })
        })
    },
    prioritysBatchs(url, ids, priorities, disableds, regDefId) { // 保存排列循序四个参数
      this.$confirm('是否保存排序？', '提示', { type: 'warning' })
        .then(mes => {
          this.$http.post(url, { ids: ids, priorities: priorities, disableds: disableds, defId: regDefId }).then(res => {
            if (res.code === '200') {
              this.successMessage('操作成功')
              this.getTableData(this.params)
            }
          })
        })
    },
    prioritysBatch(url, ids, priorities) { // 保存排列
      this.$confirm('是否保存排序？', '提示', { type: 'warning' })
        .then(mes => {
          this.$http.post(url, { ids: ids, priorities: priorities }).then(res => {
            if (res.code === '200') {
              this.successMessage('保存成功')
              this.getTableData(this.params)
            }
          })
        })
    },
    revertBatch(url, ids) { // 批量还原
      this.$confirm('是否确定还原？', '提示', { type: 'warning' })
        .then(mes => {
          this.$http.post(url, { ids: ids }).then(res => {
            if (res.code === '200') {
              this.successMessage('还原成功')
              this.getTableData(this.params)
            }
          })
        })
    },
    reviewBatch(url, ids) { // 批量审核
      this.$confirm('是否批量审核', '提示', { type: 'warning' })
        .then(mes => {
          this.$http.post(url, { ids: ids }).then(res => {
            if (res.code === '200') {
              this.successMessage('审核成功!')
              setTimeout(() => {
                this.getTableData(this.params)
              }, 800)
            }
          })
        })
    },
    setPage(event) {
      const num = event.target.value
      const re = /^[0-9]+$/ // 做一下数据过滤
      const state = re.test(num)
      if (state) {
        this.page.pageSize = parseInt(num)
      } else {
        this.page.pageSize = 30
        this.page.inputNum = 30
      }
      this.page.pageNo = 1
      this.getPages(this.page.pageNo, this.page.pageSize)
      this.$emit('change', this.page.pageNo, this.page.pageSize)
    },
    handleCurrentChange(val) {
      this.page.pageNo = val
      this.getPages(this.page.pageNo, this.page.pageSize)
      this.$emit('change', this.page.pageNo, this.page.pageSize)
    }

  }
}
